import sun.org.mozilla.javascript.internal.json.JsonParser;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;

import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.NORTH;
import static java.awt.BorderLayout.SOUTH;

/**
 * Created by blatand on 2017/4/3.
 */
public class BitMapShowDemo {

    JFrame frame=new JFrame("Select BitMap");
    JComboBox[] comboBox = new JComboBox[64+1];
    public static JPanel setJComboBox( JComboBox[] comboBox){





        int x = 0,y = 0;
        int bitType =0;
        String type[]={"LLVAR","LLLVAR","BCD_LLVAR","BCD_LLLVAR","0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"};
//        JComboBox comboBox;
        String display = new String();

//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        frame.setLocation(0,0);
//        frame.setSize(screenSize.width,screenSize.height-100);
        for(int j=0;j<64;j++){
            comboBox[j] = new JComboBox(type);
        }
        String title = new String();
        JPanel jpanel = new JPanel();
        jpanel.setLayout(new GridLayout(8,8));

        for(int j=0;j<8;j++){
            x = 0;
            for(int i=0;i<8;i++) {
                bitType = MsgUnPack.getBitMapType(j*8+(i+1),"bitmap.ini");
                switch(bitType){

                    case Msg8583Util.TYPE_LLVAR:
                        display = type[0];
                        break;
                    case Msg8583Util.TYPE_BCDLLVAR:
                        display = type[2];
                        break;
                    case Msg8583Util.TYPE_LLLVAR:
                        display = type[1];
                        break;
                    case Msg8583Util.TYPE_BCDLLLVAR:
                        display = type[3];
                        break;
                    default:
                        display = Integer.toString(bitType);
                        break;
                }
//                System.out.printf("display = %s\r\n",display);
                title = String.format("Field%03d:",j*8+i+1);

                comboBox[j*8+i].setBorder(BorderFactory.createTitledBorder(title));
                comboBox[j*8+i].setSelectedItem(display);

//                comboBox[j*8+i].setBounds(x,20,20,20);
//                x  = screenSize.width/8 * (i+1);

                jpanel.add(comboBox[j*8+i]);
            }

        }
        jpanel.setBorder(BorderFactory.createLoweredBevelBorder());
        return jpanel;
    }
    public static JPanel showMsg(String title,String text){

        JTextArea t = new JTextArea();
        JScrollPane scrollBar;
        JPanel jpanel = new JPanel();
        jpanel.setLayout(new BorderLayout());
        JLabel lb = new JLabel(title);
        lb.setFont(new Font("TimesRoman",Font.BOLD,20));
        t.setFont(new Font("TimesRoman",Font.BOLD,20));
        t.setColumns(10);
        t.setRows(5);
        t.setText(text);

        scrollBar = new JScrollPane(t);

        jpanel.add(lb,NORTH);
        jpanel.add(t,NORTH);
        jpanel.add(scrollBar,CENTER);
        jpanel.setBorder(BorderFactory.createLoweredBevelBorder());
        jpanel.setPreferredSize(new Dimension(0, 150));
        return jpanel;
    }
    public static JPanel showButton(JButton bt1,JButton bt2){

        JPanel jpanel = new JPanel();
        jpanel.setLayout(new FlowLayout());
        jpanel.add(bt1);
        jpanel.add(bt2);
        jpanel.setBorder(BorderFactory.createLoweredBevelBorder());
        return jpanel;
    }
//    private static void showBitMap(){
//
//    }
    public void show(){
        frame.setVisible(true);
    }

    public  void InitSelectBitMap(){

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JButton bt_save =new JButton("SAVE");
        JButton bt_load =new JButton("LOAD");

        frame.setLayout(null);
//        frame.setLayout(new GridLayout(3,1));
//        Container cont=new Container();
        Container cont=frame.getContentPane();

//        cont.add(showMsg(null,"N:固定长度\r\nLLVAR:长度一个字节，变长"));
        System.out.printf("width = %d  height = %d",screenSize.width,screenSize.height);
        cont.add(showMsg(null,"N:-----------固定长度\r\n" +
                              "LLVAR:-------LEN = 1，变长\r\n" +
                              "LLLVAR:------LEN = 2，变长\r\n"+
                              "BCD_LLVAR:---LEN = 1，变长压缩\r\n"+
                              "BCD_LLLVAR---LEN = 2，变长压缩")).setBounds(0,0,screenSize.width,200);


        cont.add(setJComboBox(comboBox)).setBounds(0,200,screenSize.width,500);
        cont.add(showButton(bt_save,bt_load)).setBounds(0,screenSize.height-200,screenSize.width,100);
//        frame.add(cont);
        bt_save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<64;i++){
//                    System.out.printf("comboBox[%d] = %s\r\n",i+1,comboBox[i].getSelectedItem());
//                    Object value = comboBox[i].getSelectedItem();
                    String value = new String();
                    if(comboBox[i].getSelectedItem().equals("LLVAR")){
                        value = "92";
                    }else if(comboBox[i].getSelectedItem().equals("BCD_LLVAR")){
                        value = "93";
                    }else if(comboBox[i].getSelectedItem().equals("LLLVAR")){
                        value = "94";
                    }else if(comboBox[i].getSelectedItem().equals("BCD_LLLVAR")){
                        value = "95";
                    }else{
                        value = comboBox[i].getSelectedItem().toString();
                    }
                    String section_name = String.format("Field%03d",i+1);
                    ProfileString ConfigurationFile = new ProfileString();
                    try{
                        ConfigurationFile.setProfileString("bitmap.ini" , "FieldType" , section_name , value);
                    }catch(IOException e1) {

                        e1.printStackTrace();
                    }
                }
                frame.setVisible(false);
            }


        });

        bt_load.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        frame.setSize(screenSize.width,screenSize.height);
//        System.out.printf("screenSize.width = %d,screenSize.height = %d",screenSize.width/2+100,screenSize.height/2-50);
//        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent arg0){
                System.exit(1);
            }
        });
    }

    public static void main(String[] args) {

        BitMapShowDemo bm = new BitMapShowDemo();
        bm.InitSelectBitMap();
        bm.show();

    }
}
